home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Conversion / Convert_RTF / Source / rtfToken.m < prev   
Text File  |  1993-10-02  |  9KB  |  240 lines

  1. /***********************************************************************\
  2. rtf token class for Convert RTF which converts between Mac and NeXT rtf formats.
  3. Copyright (C) 1993 David John Burrowes
  4.  
  5. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version.
  6.  
  7. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
  8.  
  9. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  10.  
  11. The author, David John Burrowes, can be reached at:
  12.     davidjohn@kira.net.netcom.com
  13.     David John Burrowes
  14.     1926 Ivy #10
  15.     San Mateo, CA 94403-1367
  16. \***********************************************************************/
  17.  
  18. #import "rtfToken.h"
  19. #import <math.h>
  20. #import <string.h>
  21. #import    <stdio.h>
  22. #import <stdlib.h>
  23.  
  24. @implementation rtfToken
  25.  
  26.  
  27. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  28. //    Routine:        init
  29. //    Parameters:    none
  30. //    Returns:        self
  31. //    Stores:        none
  32. //    Description:
  33. //        Just a simple wrapper call to initTokenOftype.
  34. //    Bugs:
  35. //    history
  36. //        93.01.02    djb    Created
  37. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  38. - init
  39. {
  40.     return [self   initTokenOfType: tokenWord];
  41. }
  42.  
  43.  
  44. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  45. //    Routine:        initTokenOfType
  46. //    Parameters:    the type of token to create.
  47. //    Returns:        self
  48. //    Stores:        none
  49. //    Description:
  50. //        Initializes a new RTF token. by setting up it's instance variables.  Two things
  51. //        are worth noting.  (1) name always points to a legal string, even if it is just a null
  52. //        length CString.  (2) There is no way to change the type of a token after it is created.
  53. //        This was deliberate.
  54. //        Given the token in an rtf file:  \margin23  the type is a tokenControlWord, the
  55. //        token's name would be `margin', it has a parameter, and the parameter is 23.
  56. //    Bugs:
  57. //    history
  58. //        93.01.02    djb    Commended and added [super init] (oops)
  59. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  60. - initTokenOfType: (TokenType) theType
  61. {
  62.     [super init];
  63.     
  64.     type = theType;
  65.     name = NewCString(0);
  66.     hasParameter = NO;
  67.     parameter = 0;
  68.     return self;
  69. }
  70.  
  71.  
  72. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  73. //    Routine:        free
  74. //    Parameters:    none
  75. //    Returns:        self
  76. //    Stores:        none
  77. //    Description:
  78. //        Imagine my horror when I realized my rtf controller was, naturally,
  79. //        allocating and destroying many a token, and happily calling free, but that there
  80. //        was no specialized free method here to free the name of the token!!!!!  YI!!!
  81. //        So, this does that.  Now we get to see if this makes the thing any less sluggish.
  82. //        I may even be currently crashing due to excessive stuff (mallocdebug reports
  83. //        alomst 6K nodes on a simple file conversion. may of which this will remove).
  84. //    Bugs:
  85. //    history
  86. //        92.12.25    djb    Created
  87. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  88. - free
  89. {
  90.     FreeCString(name);
  91.     return [super free];
  92. }
  93.  
  94.  
  95. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  96. //    Routine:        SetTokenName
  97. //    Parameters:    A Cstring to be stored as the token's name. 
  98. //    Returns:        self
  99. //    Stores:        none
  100. //    Description:
  101. //        Simply store a copy of the specified string as the name of the token.
  102. //    Bugs:
  103. //    history
  104. //        93.01.02    djb    Commented
  105. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  106. - SetTokenName: (CString) theName
  107. {
  108.     FreeCString(name);
  109.     name = NewCString(strlen(theName));
  110.     strcpy(name, theName);
  111.     return self;
  112. }
  113.  
  114. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  115. //    Routine:        SetTokenValue
  116. //    Parameters:    An integral value to be stored as the token's value 
  117. //    Returns:        self
  118. //    Stores:        none
  119. //    Description:
  120. //        Simply store the specified value in the token.
  121. //    Bugs:
  122. //    history
  123. //        93.01.02    djb    Commented
  124. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  125. - SetTokenValue: (Integer) theValue
  126. {
  127.     hasParameter = YES;
  128.     parameter = theValue;
  129.     return self;
  130. }
  131.  
  132.  
  133. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  134. //    Routine:        GetType
  135. //    Parameters:    none 
  136. //    Returns:        the type of this token.
  137. //    Stores:        none
  138. //    Bugs:
  139. //    history
  140. //        93.01.02    djb    Commented
  141. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  142. - (TokenType)  GetType
  143. {
  144.     return type;
  145. }
  146.  
  147.  
  148. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  149. //    Routine:        GetName
  150. //    Parameters:    none 
  151. //    Returns:        A copy of the name of the token
  152. //    Stores:        none
  153. //    Bugs:
  154. //    history
  155. //        93.01.02    djb    Commented
  156. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  157. - (CString)  GetName
  158. {
  159.     CString    temp = NewCString(strlen(name));
  160.     strcpy(temp, name);
  161.     return temp;
  162. }
  163.  
  164.  
  165. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  166. //    Routine:        HasValue
  167. //    Parameters:    none 
  168. //    Returns:        YES if this token has a value, no if not.
  169. //    Stores:        none
  170. //    Bugs:
  171. //    history
  172. //        93.01.02    djb    Commented
  173. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  174. - (Boolean) HasValue;
  175. {
  176.     return hasParameter;
  177. }
  178.  
  179.  
  180. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  181. //    Routine:        GetValue
  182. //    Parameters:    none 
  183. //    Returns:        The integral parameter of the token.
  184. //    Stores:        none
  185. //    Bugs:
  186. //    history
  187. //        93.01.02    djb    Commented
  188. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  189. - (Integer)  GetValue
  190. {
  191.     return parameter;
  192. }
  193.  
  194.  
  195. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  196. //    Routine:        GetLength
  197. //    Parameters:    none
  198. //    Returns:        the length of the token as if it were printed out in ascii
  199. //    Stores:        none
  200. //    Description:
  201. //        This returns the length of this token as if it were represented as an ascii
  202. //        string.  Note that this does not include the escape character or any trailing
  203. //        delimiter.  Thus, in "\fonttbl\f23 \froman" the length of the second token
  204. //        would be 3 (f23.  not the \ or the space).  The length of the first would be 7.
  205. //        Note that the log10 computation below figures out how many digits make up
  206. //        the theoretical string without actually sprinf()-ing the integer into a string.
  207. //    Bugs:
  208. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  209. - (PositiveInteger)  GetLength
  210. {
  211.     PositiveInteger    thelength    = strlen(name);
  212.     //
  213.     //    If the token was \', then it gets it's own special length, otherwise it was
  214.     //    a control symbol with a parameter (always a number) so get it's length.
  215.     //
  216.     if  (hasParameter == YES)
  217.     {
  218.         if ( (type  == tokenControlSymbol) && (strcmp(name, "\'") == 0) )
  219.             thelength += 2;
  220.         else
  221.         {
  222.             if (parameter == 0)
  223.                 thelength += 1;
  224.             else
  225.             {
  226.                 if (parameter < 0)
  227.                     thelength += (2+ floor(log10(parameter*-1))); 
  228.                 else
  229.                     thelength += (1+ floor(log10(parameter)));
  230.             }
  231.         }
  232.     }
  233.  
  234.     return thelength;
  235. }
  236.  
  237.  
  238.  
  239. @end
  240.